import dataPreferences from '@ohos.data.preferences';
import UIAbility from '@ohos.app.ability.UIAbility';
import { PreferencesData } from '@ohos/http';
import router from '@ohos.router';
import UserInfo from '../http/response/UserInfo';
import ArrayList from '@ohos.util.ArrayList';

import data_preferences from '@ohos.data.preferences';

/**
 * app 公共数据存储类
 */
class UserInfoSave {
  // 首选项名字
  private KEY_USER_INFO = 'KEY_USER_INFO';
  //首选项Key字段
  private KEY_USER_INFO_TOKEN = 'KEY_USER_INFO_TOKEN';
  private KEY_USER_INFO_HEAD = 'KEY_USER_INFO_HEAD';
  private KEY_USER_INFO_PHONE = 'KEY_USER_INFO_PHONE';
  private KEY_USER_INFO_EMAIL = 'KEY_USER_INFO_EMAIL';
  private KEY_USER_INFO_NICKNAME = 'KEY_USER_INFO_NICKNAME';
  private KEY_USER_INFO_GENDER = 'KEY_USER_INFO_GENDER';

  isLogin(needJump: boolean = true): Promise<boolean> {
    return new Promise<boolean>((resolve, reject) => {
      let token = PreferencesData.getString(this.KEY_USER_INFO, this.KEY_USER_INFO_TOKEN)
      token.then(value => {
        if (value.length > 0) {
          resolve(value.length > 0)
        } else {
          if (needJump) {
            router.pushUrl({ url: 'pages/Login' })
          }
        }
      })
    })
  }

  saveUserInfo(userInfo: UserInfo, token: string) {
    PreferencesData.putString(this.KEY_USER_INFO, this.KEY_USER_INFO_TOKEN, token)
    PreferencesData.putString(this.KEY_USER_INFO, this.KEY_USER_INFO_HEAD, userInfo.nickName)
    PreferencesData.putString(this.KEY_USER_INFO, this.KEY_USER_INFO_PHONE, userInfo.phone)
    PreferencesData.putString(this.KEY_USER_INFO, this.KEY_USER_INFO_EMAIL, userInfo.email)
    PreferencesData.putString(this.KEY_USER_INFO, this.KEY_USER_INFO_NICKNAME, userInfo.nickName)
    // PreferencesData.putString(this.KEY_USER_INFO, this.KEY_USER_INFO_GENDER, userInfo.gender)
  }

  getUserInfo(): Promise<UserInfo> {
    return new Promise<UserInfo>((resolve, reject) => {
      let userInfo = new UserInfo()
      if (this.isLogin(false)) {
        let head = PreferencesData.getString(this.KEY_USER_INFO, this.KEY_USER_INFO_HEAD)
        let phone = PreferencesData.getString(this.KEY_USER_INFO, this.KEY_USER_INFO_PHONE)
        let email = PreferencesData.getString(this.KEY_USER_INFO, this.KEY_USER_INFO_EMAIL)
        let nickname = PreferencesData.getString(this.KEY_USER_INFO, this.KEY_USER_INFO_NICKNAME)
        let gender = PreferencesData.getString(this.KEY_USER_INFO, this.KEY_USER_INFO_GENDER)
        phone.then((value) => {
          userInfo.phone = value
        })
        let reqAr3 = [head, phone, email, nickname, gender]
        // let reqAr2: ArrayList<Promise<Object>> = [ head,phone ]
        // let reqAr: ArrayList<Promise<Object>> = new ArrayList()
        // reqAr.add(head)
        // reqAr.add(phone)
        // reqAr.add(email)
        // reqAr.add(nickname)
        // reqAr.add(gender)
        Promise.all(reqAr3).then(res => {
          // res.forEach((item, index, list) => {
          // })
          // userInfo.head = res[0] as string
          userInfo.phone = res[1] as string
          userInfo.email = res[2] as string
          userInfo.nickName = res[3] as string
          userInfo.gender = res[4] as string
          resolve(userInfo)
        })
      }
    })
  }

  // createAppSP(context) {
  //   // 首选项名字
  //   const PREFERENCES_NAME = 'userInfo';
  //   globalThis.getFontPreferences = (() => {
  //     // 获取首选项实例
  //     let preferences: Promise<dataPreferences.Preferences> = dataPreferences.getPreferences(context, PREFERENCES_NAME);
  //     return preferences;
  //   });
  // }

  async getAppStartupCount() {
    let appStartupCount: number = 0;
    const preferences = await globalThis.getFontPreferences();
    appStartupCount = await preferences.get(this.KEY_USER_INFO_TOKEN, appStartupCount);
    return appStartupCount;
  }

  // saveAppStartupCount(fontSize: number) {
  //   globalThis.getFontPreferences().then((preferences) => {
  //     // 判断保存的key是否存在
  //     preferences.has(this.KEY_APP_STARTUP_COUNT).then(async (isExist) => {
  //       // Logger.info(TAG, 'preferences has changeFontSize is ' + isExist);
  //       if (!isExist) {
  //         // 保存数据
  //         await preferences.put(this.KEY_APP_STARTUP_COUNT, fontSize);
  //         preferences.flush();
  //       }
  //     }).catch((err) => {
  //       // Logger.error(TAG, 'Has the value failed with err: ' + err);
  //     });
  //   }).catch((err) => {
  //     // Logger.error(TAG, 'Get the preferences failed, err: ' + err);
  //   });
  // }


  // createAppStartupCount(context) {
  //   // 首选项名字
  //   const PREFERENCES_NAME = 'appStartupCount';
  //   globalThis.getFontPreferences = (() => {
  //     // 获取首选项实例
  //     let preferences: Promise<dataPreferences.Preferences> = dataPreferences.getPreferences(context, PREFERENCES_NAME);
  //     return preferences;
  //   });
  // }
  //
  // createAdvert(context) {
  //   // 首选项名字
  //   const PREFERENCES_NAME = 'advert';
  //   globalThis.getFontPreferences = (() => {
  //     // 获取首选项实例
  //     let preferences: Promise<dataPreferences.Preferences> = dataPreferences.getPreferences(context, PREFERENCES_NAME);
  //     return preferences;
  //   });
  // }
  //
  // async getChangeFontSize() {
  //   let fontSize: number = 0;
  //   const preferences = await globalThis.getFontPreferences();
  //   fontSize = await preferences.get(this.KEY_APP_FONT_SIZE, fontSize);
  //   return fontSize;
  // }

}

export default new UserInfoSave()